﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;

namespace luanvanFinal
{
    /// <summary>
    /// Interaction logic for wdVersion.xaml
    /// </summary>
    public partial class wdVersion : Window
    {
        
        int idProject;
        public wdVersion()
        {            
            InitializeComponent();
            loadProject();
        }

        public void loadProject()
        {
            String strSQL = "select proName from project";
            clsConnect ketnoi = new clsConnect();
            ketnoi.openConnection();
            SqlDataAdapter da = new SqlDataAdapter(strSQL, ketnoi.Connection);
            DataTable dt = new DataTable();
            da.Fill(dt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cbProject.Items.Add(dt.Rows[i][0]);
            }
            cbProject.SelectedIndex = 0;
            ketnoi.closeConnection();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            
        }

        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            datagridUser();
            datagridSystem();
            datagridDesign();
            showGroup();
        }

        public void datagridUser()
        {
            int STT = 1;
            clsProject project = new clsProject();
            idProject = Convert.ToInt32(project.getIdFromName(cbProject.Text));
            clsVersion version = new clsVersion();
            String SQL = "select idReq as ReqTittle,class as ReqType from version where idProject='"+idProject+"' and class like 'User%'";
            DataTable dt = version.getDataVersion(SQL);
            DataTable getDt = new DataTable();
            getDt.Columns.Add("Num");
            getDt.Columns.Add("Requirement Title");
            getDt.Columns.Add("Type");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = getDt.NewRow();
                dr[0] = STT.ToString();
                dr[1] = dt.Rows[i][0].ToString();
                dr[2] = dt.Rows[i][1].ToString();
                STT++;
                getDt.Rows.Add(dr);
                getDt.AcceptChanges(); 
            }
            dgUser.ItemsSource = getDt.DefaultView;
        }

        public void datagridSystem()
        {
            int STT = 1;
            clsProject project = new clsProject();
            idProject = Convert.ToInt32(project.getIdFromName(cbProject.Text));
            clsVersion version = new clsVersion();
            String SQL = "select idReq as ReqTittle,class as ReqType from version where idProject='" + idProject + "' and class like 'Sys%'";
            DataTable dt = version.getDataVersion(SQL);
            DataTable getDt = new DataTable();
            getDt.Columns.Add("Num");
            getDt.Columns.Add("Requirement Title");
            getDt.Columns.Add("Type");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = getDt.NewRow();
                dr[0] = STT.ToString();
                dr[1] = dt.Rows[i][0].ToString();
                dr[2] = dt.Rows[i][1].ToString();
                STT++;
                getDt.Rows.Add(dr);
                getDt.AcceptChanges();
            }
            dgSys.ItemsSource = getDt.DefaultView;
        }

        public void datagridDesign()
        {
            int STT = 1;
            clsProject project = new clsProject();
            idProject = Convert.ToInt32(project.getIdFromName(cbProject.Text));
            clsVersion version = new clsVersion();
            String SQL = "select idReq as ReqTittle,class as ReqType from version where idProject='" + idProject + "' and class like 'Design%'";
            DataTable dt = version.getDataVersion(SQL);
            DataTable getDt = new DataTable();
            getDt.Columns.Add("Num");
            getDt.Columns.Add("Requirement Title");
            getDt.Columns.Add("Type");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = getDt.NewRow();
                dr[0] = STT.ToString();
                dr[1] = dt.Rows[i][0].ToString();
                dr[2] = dt.Rows[i][1].ToString();
                STT++;
                getDt.Rows.Add(dr);
                getDt.AcceptChanges();
            }
            dgDesign.ItemsSource = getDt.DefaultView;
        }
        public void showGroup()
        {
            try
            {
                int STT = 1;
                String SQL = "select * from line where idProject = '" + idProject + "'";
                
                DataTable dt = clsLine.getDataLine(SQL);
                DataTable getDt = new DataTable();
                getDt.Columns.Add("Num");
                getDt.Columns.Add("User Requirement");
                getDt.Columns.Add("System Requirement");
                getDt.Columns.Add("Design Requirement");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow dr = getDt.NewRow();
                    dr[0] = STT.ToString();
                    dr[1] = dt.Rows[i][2].ToString();
                    dr[2] = dt.Rows[i][3].ToString();
                    dr[3] = dt.Rows[i][4].ToString();
                    STT++;
                    getDt.Rows.Add(dr);
                    getDt.AcceptChanges();
                }
                dgLine.ItemsSource = getDt.DefaultView;
            }
            catch
            {
            }
        }

        private void btnGhep_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataRowView rowUser = (DataRowView)dgUser.SelectedItems[0];
                String idReqUser = rowUser[1].ToString();
                DataRowView rowSys = (DataRowView)dgSys.SelectedItems[0];
                String idReqSys = rowSys[1].ToString();
                DataRowView rowDesign = (DataRowView)dgDesign.SelectedItems[0];
                String idReqDesign = rowDesign[1].ToString();
                clsLine line = new clsLine();
                line.IdProject = idProject;
                line.ClsUser = idReqUser;
                line.ClsSys = idReqSys;
                line.ClsDesign = idReqDesign;
                line.Note = new TextRange(rtbNote.Document.ContentStart, rtbNote.Document.ContentEnd).Text;
                line.insertLine();
            }
            catch
            {
                MessageBox.Show("You have not choose any row in the table !!!","Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void btnShow_Click(object sender, RoutedEventArgs e)
        {
            wdShowVersion showVersion = new wdShowVersion();
            showVersion.IdProject = idProject;
            showVersion.Show();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

       
    }

}
